package poker.gui;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import javax.swing.JButton;

public class PokerFrameChipListener implements MouseListener, MouseMotionListener{

MyCursorContainer cursorContainer = null;
	
	/*FROM MULTITOUCH CLASS*/
	FingerContainer fingerContainer = null; 
	private boolean isFingerDraggingModeActive = false;
	private int indexOfFingerUnderMouse = -1;
	private int mouse_x, mouse_y;
	public static final int TOUCH_EVENT_DOWN = 0;
	public static final int TOUCH_EVENT_MOVE = 1;
	public static final int TOUCH_EVENT_UP = 2;


	public PokerFrameChipListener(){
		fingerContainer = new FingerContainer();
	}
	
	@Override
	public void mouseDragged(MouseEvent evt) {
		mouse_x = evt.getX();
		mouse_y = evt.getY();
		if(evt.getSource().getClass().equals(JButton.class)){
			if(((JButton)evt.getSource()).getName().startsWith("p1")){
				if(((JButton)evt.getSource()).getName().startsWith("p1Chip5")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p1Chip10")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p1Chip20")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p1Chip50")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
			}
			else if(((JButton)evt.getSource()).getName().startsWith("p2")){
				if(((JButton)evt.getSource()).getName().startsWith("p2Chip5")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p2Chip10")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p2Chip20")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p2Chip50")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
			}
			else if(((JButton)evt.getSource()).getName().startsWith("p3")){
				if(((JButton)evt.getSource()).getName().startsWith("p3Chip5")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p3Chip10")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p3Chip20")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p3Chip50")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
			}
			else if(((JButton)evt.getSource()).getName().startsWith("p4")){
				if(((JButton)evt.getSource()).getName().startsWith("p4Chip5")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p4Chip10")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p4Chip20")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
				else if(((JButton)evt.getSource()).getName().startsWith("p4Chip50")){
					((JButton)evt.getSource()).setLocation((int) (((JButton)evt.getSource()).getLocation().getX() + evt.getX() - mouse_x),
							(int) (((JButton)evt.getSource()).getLocation().getY() + evt.getY() - mouse_y));
				}
			}
		}
	}
	public synchronized void mousePressed(MouseEvent e) {
		mouse_x = e.getX();
		mouse_y = e.getY();

		// multitouchFramework.requestRedraw();
	}

	public synchronized void mouseReleased(MouseEvent e) {
		mouse_x = e.getX();
		mouse_y = e.getY();

		// multitouchFramework.requestRedraw();
	}



	public synchronized void mouseMoved(MouseEvent e) {
		mouse_x = e.getX();
		mouse_y = e.getY();

		// multitouchFramework.requestRedraw();
	}
	
	public synchronized void processMultitouchInputEvent( int id, float x, float y, int type, MouseEvent e ) {
		//System.out.println("event: "+id+", "+x+", "+y+", "+type);
		int cursorIndex = cursorContainer.updateCursorById( id, x, y );

//		if ( isCameraModeActive ) {
		if ( type == MultitouchFramework.TOUCH_EVENT_UP ) {
			System.out.println("up from parent");
			cursorContainer.removeCursorByIndex( cursorIndex );
			if(e.getSource().equals(JButton.class)){
				System.out.println(((JButton) e.getSource()).getName());
				/*if(((JButton) e.getSource()).getName().equals("p1Chip5")){
					
					if(((JButton) e.getSource()).getParent().equals(JPanel.class)){
						if(((JButton) e.getSource()).getParent().getName().equals("betZonePanelP1")){
							//
						}
					}
					
				}else if(((JButton) e.getSource()).getName().equals("p1Chip10")){
					
				}else if(((JButton) e.getSource()).getName().equals("p1Chip20")){
					
				}else if(((JButton) e.getSource()).getName().equals("p1Chip50")){
					
				}else if(((JButton) e.getSource()).getName().equals("p2Chip5")){
				//ouais on testera
				}else if(((JButton) e.getSource()).getName().equals("p2Chip10")){
					
				}else if(((JButton) e.getSource()).getName().equals("p2Chip20")){
					
				}else if(((JButton) e.getSource()).getName().equals("p2Chip50")){
					
				}else if(((JButton) e.getSource()).getName().equals("p3Chip5")){
				//ouais on testera
				}else if(((JButton) e.getSource()).getName().equals("p3Chip10")){
					
				}else if(((JButton) e.getSource()).getName().equals("p3Chip20")){
					
				}else if(((JButton) e.getSource()).getName().equals("p3Chip50")){
					
				}else if(((JButton) e.getSource()).getName().equals("p4Chip5")){
				//ouais on testera
				}else if(((JButton) e.getSource()).getName().equals("p4Chip10")){
					
				}else if(((JButton) e.getSource()).getName().equals("p4Chip20")){
					
				}else if(((JButton) e.getSource()).getName().equals("p4Chip50")){
					
				}*/
			}
		}
//		
		else if ( type == MultitouchFramework.TOUCH_EVENT_MOVE ) {
			System.out.println("detected drag from parent");
//			while(cursorContainer.getNumCursors() > 0){
//			for(int index = 0; index < cursorContainer.getNumCursors(); index++){
//				Component temp = pokerFrame.getContentPane().getComponent(0).getComponentAt((int)cursorContainer.getCursorByIndex(0).getCurrentPosition().x(), 
//						(int)cursorContainer.getCursorByIndex(0).getCurrentPosition().y());
//				System.out.println(pokerFrame.getContentPane().getComponent(0).getComponentAt((int)cursorContainer.getCursorByIndex(0).getCurrentPosition().x(), 
//						(int)cursorContainer.getCursorByIndex(0).getCurrentPosition().y()).getName());
//			}
//			}
//			if(temp != null){
//				System.out.println("Recognized "+temp.toString());
//				MouseEvent m1 = new MouseEvent(temp,MouseEvent.MOUSE_DRAGGED,System.currentTimeMillis(),0,
//						(int)cursorContainer.getCursorByIndex(0).getCurrentPosition().x(),
//						(int)cursorContainer.getCursorByIndex(0).getCurrentPosition().y(),
//						1,
//						false);
//				temp.dispatchEvent(m1);
//			}
			if ( cursorContainer.getNumCursors() == 2 ) {
				System.out.println("2");
				MyCursor cursor0 = cursorContainer.getCursorByIndex( 0 );
				MyCursor cursor1 = cursorContainer.getCursorByIndex( 1 );
			}
			else if ( cursorContainer.getNumCursors() == 1 ) {
				MyCursor cursor = cursorContainer.getCursorByIndex( 0 );
				
			}
		}
		else if( type == MultitouchFramework.TOUCH_EVENT_DOWN ){
			
		}
		//multitouchFramework.requestRedraw();
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
	/*public void retrieveAndProcessFinger(MouseEvent e, int type){
		indexOfFingerUnderMouse = fingerContainer.createFinger( e.getX(), e.getY() );
		Finger f = fingerContainer.getFingerByIndex( indexOfFingerUnderMouse );
		processMultitouchInputEvent(
			f.id,
			f.position.x(),
			f.position.y(),
			type
		);
		
	}*/
	
	/*public synchronized void processMultitouchInputEvent( int id, float x, float y, int type ) {
		//System.out.println("event: "+id+", "+x+", "+y+", "+type);
		int cursorIndex = cursorContainer.updateCursorById( id, x, y );

		if ( type == MultitouchFramework.TOUCH_EVENT_UP ) {
			// up event
			System.out.println("WM TOUCH EVENT UP");
			cursorContainer.removeCursorByIndex( cursorIndex );
		}
		else if ( type == MultitouchFramework.TOUCH_EVENT_MOVE ) {
			System.out.println(" WM TOUCH EVENT MOVE");
			if ( cursorContainer.getNumCursors() == 2 ) {
				System.out.println("2");
				MyCursor cursor0 = cursorContainer.getCursorByIndex( 0 );
				MyCursor cursor1 = cursorContainer.getCursorByIndex( 1 );
				gw.panAndZoomBasedOnDisplacementOfTwoPoints(
					id==cursor0.id ? cursor0.getPreviousPosition() : cursor0.getCurrentPosition(),
					id==cursor1.id ? cursor1.getPreviousPosition() : cursor1.getCurrentPosition(),
					cursor0.getCurrentPosition(),
					cursor1.getCurrentPosition()
				);
			}
			else if ( cursorContainer.getNumCursors() == 1 ) {
				MyCursor cursor = cursorContainer.getCursorByIndex( 0 );
				gw.pan(
					cursor.getCurrentPosition().x() - cursor.getPreviousPosition().x(),
					cursor.getCurrentPosition().y() - cursor.getPreviousPosition().y()
				);
			}
		}
		else if ( type == MultitouchFramework.TOUCH_EVENT_DOWN){
			System.out.println("WM TOUCH EVENT DOWN");
		}
	}*/
	
}
